var express = require("express");
var router = express();
// var request = require("request");
// var jwt = require("jsonwebtoken");
var db = require("../config/mysql");
var vertoken = require("../config/token");
// var { appid, appSecret } = require("../config/wx");

/**
 * @api {post} /api/users/shopcat/delete 通过id删除购物车
 * @apiDescription 通过id删除购物车
 * @apiName shopcatDelete
 * @apiGroup users
 *
 * @apiParam {id} id 购物车id
 *
 * @apiSampleRequest api/users/shopcat/delete
 *
 */

router.post("/shopcat/delete", function (req, res) {
  const { id } = req.body;
  var sql = `
  DELETE 
  FROM
    shopcat 
  WHERE
    shopcat.id = ?
  `;
  db.query(sql, [id], function (results, fields) {
    if (results.affectedRows > 0) {
      res.json({
        status: true,
        msg: "删除成功",
      });
    } else {
      res.json({
        status: false,
        msg: "删除失败",
      });
    }
  });
});

/**
 * @api {post} /api/users/order/delete 通过id删除订单
 * @apiDescription 通过id删除订单
 * @apiName orderDelete
 * @apiGroup users
 *
 * @apiParam {id} id 订单id
 *
 * @apiSampleRequest api/users/order/delete
 *
 */

router.post("/order/delete", function (req, res) {
  const { id } = req.body;
  var sql = `
  DELETE 
  FROM
    orders 
  WHERE
    orders.id = ?
  `;
  db.query(sql, [id], function (results, fields) {
    if (results.affectedRows > 0) {
      res.json({
        status: true,
        msg: "删除成功",
      });
    } else {
      res.json({
        status: false,
        msg: "删除失败",
      });
    }
  });
});

/**
 * @api {post} /api/users/order/add 添加订单
 * @apiDescription 添加订单
 * @apiName orderAdd
 * @apiGroup users
 *
 * @apiParam {int} user_id 用户id
 * @apiParam {int} good_id 物品id
 * @apiParam {int} count 物品数量
 * @apiParam {int} totalvalue 订单总价
 * @apiParam {int} status 订单状态
 *
 * @apiSampleRequest api/users/order/add
 *
 */
router.post("/order/add", function (req, res) {
  var { user_id, good_id, count, totalvalue, status } = req.body;
  var sql = `
  INSERT INTO orders (
    user_id,
    good_id,
    count,
    totalvalue,
    status
  )
  VALUES
    (
      ?,
      ?,
      ?,
      ?,
      ?
    )
  `;
  if (user_id !== "") {
    db.query(
      sql,
      [user_id, good_id, count, totalvalue, status],
      function (results, fields) {
        if (results.affectedRows > 0) {
          res.json({
            status: true,
            id: results.insertId,
            msg: "添加成功",
          });
        } else {
          res.json({
            status: false,
            msg: "添加失败",
          });
        }
      }
    );
  }
});

/**
 * @api {get} /api/users/order/:id 获取用户订单
 * @apiDescription 获取用户订单
 * @apiName order
 * @apiGroup users
 * @apiParam {int} id 用户id
 *
 * @apiSampleRequest api/users/shopcat/
 *
 */
router.get("/order/:id", function (req, res) {
  var { id } = req.params;
  // var { id } = req.body;
  // console.log(req.query);
  // console.log(req.body);
  // console.log(req.params);
  // 查询账户数据
  var sql = `
  SELECT
    songs.name, 
    songs.pic, 
    orders.totalvalue, 
    orders.count, 
    orders.id
  FROM
    orders
    INNER JOIN
    users
    ON 
      orders.user_id = users.id
    INNER JOIN
    songs
    ON 
      orders.good_id = songs.id
  WHERE
    users.id = ?
  `;
  db.query(sql, [id], function (results, fields) {
    if (results.length) {
      res.json({
        status: true,
        data: results,
        msg: "获取成功",
      });
    } else {
      res.json({
        status: false,
        msg: "获取失败",
      });
    }
  });
});

/**
 * @api {post} /api/users/shopcat/add 添加购物车
 * @apiDescription 添加购物车
 * @apiName shopcatAdd
 * @apiGroup users
 *
 * @apiParam {int} user_id 用户id
 * @apiParam {int} good_id 物品id
 * @apiParam {int} count 物品数量
 *
 * @apiSampleRequest api/users/shopcat/add
 *
 */

router.post("/shopcat/add", function (req, res) {
  var { user_id, good_id, count } = req.body;
  var sql = `
  INSERT INTO shopcat (
    user_id,
    good_id,
    count
  )
  VALUES
    (
      ?,
      ?,
      ?
    )
  `;
  if (user_id !== "") {
    db.query(sql, [user_id, good_id, count], function (results, fields) {
      if (results.affectedRows > 0) {
        res.json({
          status: true,
          id: results.insertId,
          msg: "添加成功",
        });
      } else {
        res.json({
          status: false,
          msg: "添加失败",
        });
      }
    });
  }
});

/**
 * @api {get} /api/users/shopcat/:id 获取用户购物车
 * @apiDescription 获取用户购物车
 * @apiName shopcat
 * @apiGroup users
 * @apiParam {int} id 用户id
 *
 * @apiSampleRequest api/users/shopcat/
 *
 */

router.get("/shopcat/:id", function (req, res) {
  var { id } = req.params;
  // var { id } = req.body;
  // console.log(req.query);
  // console.log(req.body);
  // console.log(req.params);
  // 查询账户数据
  var sql = `
  SELECT
    shopcat.id,
    songs.name,
    songs.artist,
    songs.pic,
    songs.presentprice,
    shopcat.count,
    users.id As user_id,
    songs.id As good_id
  FROM
    shopcat
    INNER JOIN songs ON shopcat.good_id = songs.id
    INNER JOIN users ON shopcat.user_id = users.id 
  WHERE
    users.id = ?
  `;
  db.query(sql, [id], function (results, fields) {
    if (results.length) {
      res.json({
        status: true,
        data: results,
        msg: "获取成功",
      });
    } else {
      res.json({
        status: false,
        msg: "获取失败",
      });
    }
  });
});

/**
 * @api {post} /api/users/userinfo 获取用户信息
 * @apiDescription 用户登录
 * @apiName userinfo
 * @apiGroup users
 * @apiParam {Sting} username 用户名
 *
 * @apiSampleRequest api/users/userinfo
 *
 */

router.post("/userinfo", function (req, res) {
  var { username } = req.body;
  // 查询账户数据
  var sql = `SELECT id,vip,nickName FROM users WHERE username = ?`;
  db.query(sql, [username], function (results, fields) {
    if (results.length) {
      res.json({
        status: true,
        data: results,
        msg: "登录成功",
      });
    } else {
      res.json({
        status: false,
        msg: "登录失败",
      });
    }
  });
});

/**
 * @api {post} /api/users/login 用户登录
 * @apiDescription 用户登录
 * @apiName login
 * @apiGroup users
 * @apiParam {Sting} username 用户名
 * @apiParam {Sting} password 用户密码
 *
 * @apiSampleRequest api/users/login
 *
 */

router.post("/login", function (req, res) {
  var { username, password } = req.body;
  // 查询账户数据
  var sql = `SELECT * FROM users WHERE username = ? AND password = ? `;
  db.query(sql, [username, password], function (results, fields) {
    if (results.length) {
      if (
        results[0].username === username &&
        results[0].password === password
      ) {
        vertoken.setToken(username, results[0].id).then((data) => {
          res.json({
            status: true,
            username: username,
            id: results[0].id,
            token: data,
            msg: "登录成功",
          });
        });
      }
    } else {
      res.json({
        status: false,
        msg: "登录失败",
      });
    }
  });
});

module.exports = router;
